package com.jxs.android.cmcc.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jxs.android.cmcc.boot.SelfApplication;
import com.jxs.android.cmcc.data.KipInfo;

public class KipHolder {

	static public interface Table {
		public static final String TABLE_NAME = "system_kip_tab";

		public static final String KIP_ID = "kpi_id";
		public static final String KIP_FLAG_DATE = "kpi_flag_date";

		public static final String KIP_STATE_DATE = "stat_date";
		public static final String KIP_WEEK_NAME = "week_name";

		public static final String KIP_NAME = "kpi_name";
		public static final String KIP_NAME_1 = "kpi_name_1";
		public static final String KIP_NAME_2 = "kpi_name_2";
		public static final String KIP_NAME_3 = "kpi_name_3";
		public static final String KIP_VALUE_1 = "kpi_value_1";
		public static final String KIP_VALUE_2 = "kpi_value_2";
		public static final String KIP_VALUE_3 = "kpi_value_3";

		public static final String[] COLUMNS = new String[] { KIP_ID, KIP_STATE_DATE, KIP_FLAG_DATE, KIP_WEEK_NAME, KIP_NAME, KIP_NAME_1, KIP_NAME_2, KIP_NAME_3, KIP_VALUE_1, KIP_VALUE_2, KIP_VALUE_3 };
	}

	public static void creater(SQLiteDatabase db) {
		StringBuffer createSQL = new StringBuffer(" CREATE TABLE IF NOT EXISTS ");
		createSQL.append(Table.TABLE_NAME);
		createSQL.append(" (_id INTEGER PRIMARY KEY AUTOINCREMENT, ");
		createSQL.append(Table.KIP_ID + " varchar(20), ");
		createSQL.append(Table.KIP_STATE_DATE + " varchar(20), ");
		createSQL.append(Table.KIP_FLAG_DATE + " varchar(20), ");
		createSQL.append(Table.KIP_WEEK_NAME + " varchar(20), ");
		createSQL.append(Table.KIP_NAME + " varchar(20), ");
		createSQL.append(Table.KIP_NAME_1 + " varchar(20), ");
		createSQL.append(Table.KIP_NAME_2 + " varchar(20), ");
		createSQL.append(Table.KIP_NAME_3 + " varchar(20), ");
		createSQL.append(Table.KIP_VALUE_1 + " varchar(20), ");
		createSQL.append(Table.KIP_VALUE_2 + " varchar(20), ");
		createSQL.append(Table.KIP_VALUE_3 + " varchar(20) ");
		createSQL.append(" )");
		db.execSQL(createSQL.toString());
	}

	public static void saveOrUpdate(String date, KipInfo kipInfo) {
		ContentResolver contentResolver = SelfApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		cursor = contentResolver.query(KipProvider.CONTENT_URI, Table.COLUMNS, Table.KIP_ID + "=?  and " + Table.KIP_FLAG_DATE + " =? ", new String[] { kipInfo.getKpi_id(), date }, null);
		ContentValues values = new ContentValues();

		values.put(Table.KIP_ID, kipInfo.getKpi_id());
		values.put(Table.KIP_FLAG_DATE, date);
		values.put(Table.KIP_STATE_DATE, kipInfo.getStat_date());
		values.put(Table.KIP_WEEK_NAME, kipInfo.getWeek_name());
		values.put(Table.KIP_NAME, kipInfo.getKpi_name());
		values.put(Table.KIP_NAME_1, kipInfo.getKpi_name_1());
		values.put(Table.KIP_NAME_2, kipInfo.getKpi_name_2());
		values.put(Table.KIP_NAME_3, kipInfo.getKpi_name_3());
		values.put(Table.KIP_VALUE_1, kipInfo.getKpi_value_1());
		values.put(Table.KIP_VALUE_2, kipInfo.getKpi_value_2());
		values.put(Table.KIP_VALUE_3, kipInfo.getKpi_value_3());

		if (null != cursor && cursor.getCount() == 0) {
			contentResolver.insert(KipProvider.CONTENT_URI, values);
		} else {
			contentResolver.update(KipProvider.CONTENT_URI, values, Table.KIP_ID + "=? and " + Table.KIP_FLAG_DATE + " =? ", new String[] { kipInfo.getKpi_id(), date });
		}
		if (null != cursor) {
			cursor.close();
		}
	}

	public static void deleteKipInfo(String kipId) {
		ContentResolver contentResolver = SelfApplication.getApplication().getContentResolver();
		contentResolver.delete(KipProvider.CONTENT_URI, Table.KIP_ID + " =? ", new String[] { kipId });
	}

	public static List<KipInfo> getKipInfos(String date) {
		List<KipInfo> results = new ArrayList<KipInfo>();
		ContentResolver contentResolver = SelfApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		cursor = contentResolver.query(KipProvider.CONTENT_URI, Table.COLUMNS, Table.KIP_FLAG_DATE + "=? ", new String[] { date }, null);
		if (null != cursor && cursor.moveToFirst()) {
			do {
				KipInfo kipInfo = new KipInfo();
				kipInfo.setKpi_id(cursor.getString(cursor.getColumnIndex(Table.KIP_ID)));
				kipInfo.setStat_date(cursor.getString(cursor.getColumnIndex(Table.KIP_STATE_DATE)));
				kipInfo.setWeek_name(cursor.getString(cursor.getColumnIndex(Table.KIP_WEEK_NAME)));
				kipInfo.setKpi_name(cursor.getString(cursor.getColumnIndex(Table.KIP_NAME)));
				kipInfo.setKpi_name_1(cursor.getString(cursor.getColumnIndex(Table.KIP_NAME_1)));
				kipInfo.setKpi_name_2(cursor.getString(cursor.getColumnIndex(Table.KIP_NAME_2)));
				kipInfo.setKpi_name_3(cursor.getString(cursor.getColumnIndex(Table.KIP_NAME_3)));
				kipInfo.setKpi_value_1(cursor.getString(cursor.getColumnIndex(Table.KIP_VALUE_1)));
				kipInfo.setKpi_value_2(cursor.getString(cursor.getColumnIndex(Table.KIP_VALUE_2)));
				kipInfo.setKpi_value_3(cursor.getString(cursor.getColumnIndex(Table.KIP_VALUE_3)));
				results.add(kipInfo);
			} while (cursor.moveToNext());
		}
		if (null != cursor) {
			cursor.close();
		}
		return results;
	}

}
